package com.steven.im.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.squareup.picasso.Picasso;
import com.steven.im.App;
import com.steven.im.R;
import com.steven.im.utils.FormattedDataUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * Created by Steven on 2019/1/29.
 */
public class PastSuccessAdapter extends BaseQuickAdapter<Object, BaseViewHolder> {
    private Context mContext;
    private List<Object> list;

    public PastSuccessAdapter(Context mContext, @Nullable List<Object> data) {
        super(R.layout.past_success_item, data);
        this.list = data;
        this.mContext = mContext;
    }

    @Override
    protected void convert(BaseViewHolder helper, Object item) {
        JSONObject jsonObject = (JSONObject) (JSON.toJSON(list.get(helper.getPosition())));
        Picasso.with(App.getContext()).load(jsonObject.getString("userAvatar")).into((ImageView) helper.getView(R.id.good_img));
        helper.setText(R.id.tv_nickname, jsonObject.getString("userName"));
        helper.setText(R.id.tv_time, timeStamp2Date(jsonObject.getJSONObject("addTime").getString("time")));
        helper.setText(R.id.market_money, "￥" + jsonObject.getString("originalPrice"));
        helper.setText(R.id.pay_money, "￥" + jsonObject.getString("discountPrice"));
        helper.setText(R.id.tv_discountrate, FormattedDataUtils.decimalToPercentage(jsonObject.getString("discountRate")));
    }

    private static String timeStamp2Date(String seconds) {
        @SuppressLint("SimpleDateFormat") SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        long now = Long.parseLong(seconds);      //获取当前时间，毫秒形式
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(now);
        return format.format(calendar.getTime());
    }

}
